﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TFXK.BLL;
using System.Data;
using log4net;
using TFXK.Model;
using TFXK.Common;

public partial class MyTravel : System.Web.UI.Page
{
    private static readonly ILog log = LogManager.GetLogger(typeof(MyTravel));
    private static readonly OrdersBLL bll = new OrdersBLL();
    private static readonly CustomsBLL bllCustoms = new CustomsBLL();
    public static string pro = "''";
    public static string cty = "''";
    public static string area = "''";
    public DataSet ds = new DataSet();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!CheckLogin())
            {
                Msg.ShowAndRedirect("未登录!请先登录", "Default.aspx");
            }
            if (!IsPostBack)
            {

            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion


    #region 分页绑定
    private void InitData(int PageIndex)
    {
        this.Title = "列表";    // 设置标题

        DataSet ds = new DataSet();
        int recordcount = 0; //获取总条数

        ds = bll.GetListByCID(AspNetPager1.PageSize, PageIndex, ((Customs)Session["UserLogin"]).id, -1, -1, out recordcount);

        AspNetPager1.RecordCount = recordcount;
        this.rptOrders.DataSource = ds.Tables[0].DefaultView;
        this.rptOrders.DataBind();
    }
    #endregion

    #region 分页控件加载
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                InitData(e.NewPageIndex);
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    public bool CheckLogin()
    {
        try
        {
            Customs tempModel = (Customs)Session["UserLogin"];
            if (tempModel == null)
            {
                return false;
            }
            else
            {
                txtEmail.Value = tempModel.userEmail;
                txtloginName.Value = tempModel.loginName;
                txtTel.Value = tempModel.userPhone;
                txtTrueName.Value = tempModel.trueName;
                if (!string.IsNullOrEmpty(tempModel.userCity))
                {
                    try
                    {
                        string[] citys = tempModel.userCity.Split('-');
                        pro = "'" + citys[0] + "'";
                        cty = "'" + citys[1] + "'";
                        area = "'" + citys[2] + "'";
                    }
                    catch { }
                }
                if (tempModel.userSex == 0)
                {
                    rbtnBoy.Checked = true;
                }
                else
                {
                    rbtnGirl.Checked = false;
                }
                return true;
            }
        }
        catch
        {
            return false;
        }
    }

    public string GetLineName(object pid, object tid)
    {
        try
        {
            if (tid.ToString().Equals("0"))
            {
                HotelBLL bllHotel = new HotelBLL();
                return "<a href='hotelInfo.aspx?Key=" + pid + "'>" + bllHotel.GetModel(int.Parse("" + pid)).title + "</a>";
            }
            else
            {
                TravelLinesBLL tbll = new TravelLinesBLL();
                return "<a href='GN_TravelInfo.aspx?Key=" + pid + "'>" + tbll.GetModel(int.Parse(pid + "")).title + "</a>";
            }
        }
        catch
        {
            return "获取失败";
        }
    }

    protected void btnSaveBase_Click(object sender, EventArgs e)
    {
        string province = StringUtil.HtmlEncode(Request.Form["province"] + "");
        string city = StringUtil.HtmlEncode(Request.Form["city"] + "");
        string area = StringUtil.HtmlEncode(Request.Form["area"] + "");
        string trueName = StringUtil.HtmlEncode(txtTrueName.Value + "");
        string phone = StringUtil.HtmlEncode(txtTel.Value);
        string email = StringUtil.HtmlEncode(txtEmail.Value);


        if (trueName.Length > 0)
        {

            Customs temp = bllCustoms.GetModel(((Customs)Session["UserLogin"]).id);

            if (province.Contains("选择"))
            {
                temp.userCity = "";
            }
            else
            {
                temp.userCity = province + "-" + city + "-" + area;
            }
            temp.trueName = trueName;
            temp.userEmail = email;
            temp.userPhone = phone;
            temp.userSex = rbtnBoy.Checked ? 0 : 1;
            bllCustoms.Update(temp);
            Session["UserLogin"] = temp;
            Session.Timeout = 120;
            Msg.ShowAndRedirect("更新成功!", "MyTravel.aspx");

        }
        else
        {
            Msg.Show("更新失败!稍后重试!");
        }


    }



    protected void btnSavePass_Click(object sender, EventArgs e)
    {
        string oldPass = StringUtil.HtmlEncode(txtOldPass.Value);
        string NewPass = StringUtil.HtmlEncode(txtNewPass1.Value);
        string PassAgn = StringUtil.HtmlEncode(txtNewPass2.Value);

        if (string.IsNullOrEmpty(oldPass) || string.IsNullOrEmpty(NewPass))
        {
            Msg.Show("密码不能为空！");
        }
        else
        {
            if (NewPass.Equals(PassAgn))
            {
                if (bllCustoms.Exists(((Customs)Session["UserLogin"]).loginName, oldPass))
                {
                    Customs temps = new Customs();
                    temps.loginName = ((Customs)Session["UserLogin"]).loginName;
                    temps.loginPass = NewPass;
                    bllCustoms.UpdatePassword(temps);
                    Msg.ShowAndRedirect("更新密码成功！", "MyTravel.aspx");
                }
                else
                {
                    Msg.Show("原始密码不正确！");
                }
            }
            else
            {
                Msg.Show("两次密码输入不正确！");
            }
        }



    }
    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("Default.aspx", false);
    }

}
